//
//  ChatProtocol.m
//  供销宝
//
//  Created by 金晶 on 17/3/17.
//  Copyright © 2017年 金晶. All rights reserved.
//

#import "ChatProtocol.h"
#import "ChatManager.h"

@interface ChatProtocol ()
{
    __block NSMutableArray *singleResults;  //单聊
}

@property (nonatomic,strong) NSMutableArray *souceArray;
@property (nonatomic,strong) NSMutableArray *contactArray;
@property (nonatomic,assign) CGSize fileSize;

@property (nonatomic,strong)  NSMutableArray *results;
/**所有含图片的聊天记录*/
@property (nonatomic,strong) __block NSMutableArray *images;

@property (nonatomic,strong) NSMutableArray *chatSourceArray;

@end

@implementation ChatProtocol


#pragma mark - 获取数据
//获取和friendid的葱下标为count开始的一个page
- (NSMutableArray *)queryChatList:(NSString *)friendid curCount:(NSInteger)count {
    return [NSMutableArray arrayWithArray:[[ChatManager sharedInstance] selectDataFromTableBy:friendid currentCount:count]];
}


#pragma mark - 网络上传
- (void)uploadStreamarameter:(NSDictionary *)parameter withStreamType:(id)stream {
    
    NSDictionary *parameters = parameter[@"parameter"];
    
    if([stream isKindOfClass:[UIImage class]]) {
        CGFloat width_File =[(UIImage *)stream size].width/5;
        CGFloat height_File = [(UIImage *)stream size].height/5;
        CGSize size = CGSizeMake(width_File, height_File);
        self.fileSize = size;
        
        NSDictionary *dict = [self spliceDataParameter:parameter withMessageType:ChatMsgBodyType_Image];
#warning 发消息
//        [[WebSocketManage webSocketManage] sendMessageOperation:dict withChatType:[parameter[@"chatType"] intValue]];
    } else if([stream isKindOfClass:[NSURL class]]){
        NSDictionary *dict = [self spliceDataParameter:parameter withMessageType:ChatMsgBodyType_Voice];
#warning 发消息

//        [[WebSocketManage webSocketManage] sendMessageOperation:dict withChatType:[parameter[@"chatType"] intValue]];
    }
#warning 需要上传到服务器啊,这里该怎么弄?
}

- (NSDictionary *)spliceDataParameter:(NSDictionary *)parameter withMessageType:(ChatMsgBodyType)messageType {
    
    if ([parameter[@"chatType"] intValue] == ChatTypePlan) {
        //单聊
        if(messageType ==ChatMsgBodyType_Image) {
            
            return @{@"message":parameter[@"parameter"][@"fileCode"],@"msgId":parameter[@"msgId"],@"fromID":parameter[@"fromID"],@"MessageType":@(messageType),@"width":@(self.fileSize.width),@"height":@(self.fileSize.height)};
            
        }else if (messageType ==ChatMsgBodyType_Voice){
            
            return @{@"message":parameter[@"parameter"][@"fileCode"],@"msgId":parameter[@"msgId"],@"fromID":parameter[@"fromID"],@"MessageType":@(messageType),@"duringTime":parameter[@"duringTime"]};
            
        }else {
            
            return @{@"message":parameter[@"parameter"][@"message"],@"msgId":parameter[@"msgId"],@"fromID":parameter[@"fromID"],@"MessageType":@(messageType)};
        }
    }else {
        
        return @{};
    }
}

- (void)senderText:(NSDictionary *)message {
    //websock发送消息信息需要传对象
//    NSDictionary *dict = [self spliceDataParameter:message withMessageType:ChatMsgBodyType_Text];
#warning 发送消息业务
//    [[WebSocketManage webSocketManage] sendMessageOperation:dict withChatType:[message[@"chatType"] intValue]];
}


//加载缓存时是否显示小菊米
- (BOOL)isShowActivityView:(NSInteger)start {
    
    NSInteger total = self.results.count-start*LOAD_NUMBER;
    if(total>LOAD_NUMBER) {
        return YES;
    }else {
        return NO;
    }
}




//收到消息时，通知刷新前。需要改变数据库状态，变为已读
- (void)uploadMessageStatus:(NSString *)friendid {
#warning 置为已读
    
}


@end
